//
//  UIViewAdditions.m
//  MTFramework
//
//  Created by 李 帅 on 12-2-15.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UIViewAdditions.h"

@implementation UIView (Geometry)

- (void)setTop:(CGFloat)t
{
    self.frame = CGRectMake(self.left, t, self.width, self.height);
}

- (CGFloat)top
{
    return self.frame.origin.y;
}

- (void)setBottom:(CGFloat)b
{
    self.frame = CGRectMake(self.left, b - self.height, self.width, self.height);
}

- (CGFloat)bottom
{
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setLeft:(CGFloat)l
{
    self.frame = CGRectMake(l, self.top, self.width, self.height);
}

- (CGFloat)left
{
    return self.frame.origin.x;
}

- (void)setRight:(CGFloat)r
{
    self.frame = CGRectMake(r - self.width, self.top, self.width, self.height);
}

- (CGFloat)right
{
    return self.frame.origin.x + self.frame.size.width;
}

/**
 * view.X属性  可获取X坐标  可设置X坐标
 */
- (void)setXPosition:(CGFloat)xPosition {
    self.frame = CGRectMake(xPosition, self.yPosition, self.width, self.height);
}

- (CGFloat)xPosition {
    return self.frame.origin.x;
}

/**
 * view.Y属性  可获取Y坐标  可设置Y坐标
 */
- (void)setYPosition:(CGFloat)yPosition {
    self.frame = CGRectMake(self.xPosition, yPosition, self.width, self.height);
}

- (CGFloat)yPosition {
    return self.frame.origin.y;
}

/**
 * view.宽度属性  可获取width  可设置width
 */
- (void)setWidth:(CGFloat)w {
    self.frame = CGRectMake(self.xPosition, self.yPosition, w, self.height);
}

- (CGFloat)width {
    return self.frame.size.width;
}

/**
 * view.高度属性  可获取height  可设置height
 */
- (void)setHeight:(CGFloat)h {
    self.frame = CGRectMake(self.xPosition, self.yPosition, self.width, h);
}

- (CGFloat)height {
    return self.frame.size.height;
}

/**
 * view.center.x属性 可获取centerX  可设置centerX
 */
- (void)setCenterX:(CGFloat)centerX {
    self.center = CGPointMake(centerX, self.centerY);
}

- (CGFloat)centerX {
    return self.center.x;
}

/**
 * view.center.y属性 可获取centerY  可设置centerY
 */
- (void)setCenterY:(CGFloat)centerY {
    self.center = CGPointMake(self.centerX, centerY);
}

- (CGFloat)centerY {
    return self.center.y;
}


/**
 * view.size属性 可获取size  可设置size
 */
- (void)setSize:(CGSize)size {
    CGRect frame = [self frame];
    frame.size.width = round(size.width);
    frame.size.height = round(size.height);
    [self setFrame:frame];
}

- (CGSize)size {
    return self.frame.size;
}

/**
 * view.origin属性 可获取origin  可设置origin
 */
- (void)setOrigin:(CGPoint)origin {
    CGRect frame = [self frame];
    frame.origin = origin;
    [self setFrame:frame];
}

- (CGPoint)origin {
    return self.frame.origin;
}

- (void)bringToFront {
    [self.superview bringSubviewToFront:self];
}

- (void)sendToBack {
    [self.superview sendSubviewToBack:self];
}

@end



@implementation UIView (ViewHiarachy)

- (UIViewController *)viewController
{
    for (UIView *next = self; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

- (void)removeAllSubviews
{
    for (UIView *subview in self.subviews) {
        [subview removeFromSuperview];
    }
}

@end


@implementation UIView (Gesture)

- (void)addTapAction:(SEL)tapAction target:(id)target
{
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:target action:tapAction];
    gesture.cancelsTouchesInView = NO;
    [self addGestureRecognizer:gesture];
}

@end

@implementation UIView (firstResponder)

- (UIView *)findViewThatIsFirstResponder
{
	if (self.isFirstResponder) {
		return self;
	}
    
	for (UIView *subView in self.subviews) {
		UIView *firstResponder = [subView findViewThatIsFirstResponder];
		if (firstResponder != nil) {
			return firstResponder;
		}
	}
	return nil;
}

- (NSArray *)descendantViews
{
    NSMutableArray *descendantArray = [NSMutableArray array];
    for (UIView *view in self.subviews) {
        [descendantArray addObject:view];
        [descendantArray addObjectsFromArray:[view descendantViews]];
    }
    return [descendantArray copy];
}

@end

@implementation UIView (autoLayout)

- (void) testAmbiguity
{
    if (self.hasAmbiguousLayout) {
        NSLog(@"<%@:%p>: %@", self.class.description, self, @"Ambiguous");
    }
    for (UIView *view in self.subviews) {
        [view testAmbiguity];
    }
}

@end

@implementation UIView (DDFramework)
- (void)cutCorners
{
    CALayer *layer = [self layer];
    layer.cornerRadius = CGRectGetWidth(self.bounds)/2;
    self.clipsToBounds = YES;
}

- (void)cutCornersWithRadius:(CGFloat)radius
{
    CALayer *layer = [self layer];
    layer.cornerRadius = radius;
    self.clipsToBounds = YES;
}
@end

@implementation UIView (EventLink)

//为UIView扩展一个方法，用于响应事件链
-(UIViewController *)viewController {
    UIResponder *nexRes=[self nextResponder];
    do {
        //判读当前的响应者是否UIViewController
        if ([nexRes isKindOfClass:[UIViewController class]]) {
            //是否直接处理
            return  (UIViewController*)nexRes;
        }else{
            //否则继续寻找
            nexRes=[nexRes nextResponder];
            
        }
    } while (nexRes!=nil);
    return nil;
}

//为UIView扩展一个方法，用于响应事件链  查找tabbar
-(UIViewController *)tabBarRootController {
    UIResponder *nexRes=[self nextResponder];
    do {
        //判读当前的响应者是否UIViewController
        if ([nexRes isKindOfClass:[UITabBarController class]]) {
            //是否直接处理
            return  (UITabBarController*)nexRes;
        }else{
            //否则继续寻找
            nexRes=[nexRes nextResponder];
            
        }
    } while (nexRes!=nil);
    return nil;
}

//为UIView扩展一个方法，用于响应事件链  查找UINavigationController
-(UINavigationController *)rootNavgationController {
    UIResponder *nexRes=[self nextResponder];
    do {
        //判读当前的响应者是否UIViewController
        if ([nexRes isKindOfClass:[UINavigationController class]]) {
            //是否直接处理
            return  (UINavigationController*)nexRes;
        }else{
            //否则继续寻找
            nexRes=[nexRes nextResponder];
            
        }
    } while (nexRes!=nil);
    return nil;
}
@end
